﻿using UnityEngine;
using System.Collections;

public class GUIController : MonoBehaviour {
	public GameStateManager gameStateManager;
	public Camera MainCamera;
	public GUIObject MainMenu, Planet1Menu, OptionsMenu, LoadingMenu;
	float aspectRatio;
	// Background stuff
	public MeshRenderer background;
	bool backgroundScroll;
	public float scrollLength, maxScrollSpeed, scrollAcceleration;
	bool scrollAccelerationFound;
	float scrollStartTime, scrollStopTime, currentScrollSpeed, scrollAccelerationTime;
	public bool scrollDirection;
	public float GUIScrollSpeed;

	float previousStartUpTime, deltaTime;

	// Button press stuff
	public GUIObject currentMenu;
	public GUIObject lastMenu;
	public GUIObject nextMenu;
	public bool deactiveLastMenu;

	// Use this for initialization
	void Start () {
		scrollAccelerationFound = false;
		backgroundScroll = false;
		Debug.Log ("Screen height: " + Screen.height);
		Debug.Log ("Screen width: " + Screen.width);
		aspectRatio = Mathf.Round (((float)Screen.width / (float)Screen.height) * 10f) / 10f;
		Debug.Log ("Aspect ratio: " + aspectRatio);
		MainCamera.orthographicSize = (4.167f * aspectRatio * aspectRatio - 20.4167f * aspectRatio + 35.5f);
	}

	public float getCurrentScrollSpeed() {
		return currentScrollSpeed;
	}

	public void setBackgroundScroll(bool backgroundScroll) {
		this.backgroundScroll = backgroundScroll;
		scrollAccelerationTime = 0f;
		currentScrollSpeed = 0f;
		scrollAccelerationFound = false;
		scrollStartTime = Time.time;
		scrollStopTime = scrollStartTime + scrollLength;
	}

	public void sendButtonPress(GUIObject button) {
		switch(button.name) {
		case "PlayButton":
			scrollDirection = true;
			setBackgroundScroll(true);
			break;
		case "LevelSelectBack":
			scrollDirection = false;
			setBackgroundScroll(true);
			break;
		case "OptionsButton":
			scrollDirection = false;
			setBackgroundScroll(true);
			break;
		case "OptionsMenuBack":
			scrollDirection = true;
			setBackgroundScroll(true);
			break;
		case "Planet1Level1":
			scrollDirection = true;
			setBackgroundScroll(true);
			break;
		}
		lastMenu = currentMenu;
		currentMenu = button.targetMenu;
		setGUIObjectActive(currentMenu, true);
		if(button.targetMenu.name == "LoadingMenu") {
			gameStateManager.setGameState(GameState.LOADING);
		}
		// Debug printing
		/*
		if(lastMenu != null) {
			Debug.Log ("Last Menu: " + lastMenu.name);
		} else {
			Debug.Log ("Last Menu: none");
		}
		if(currentMenu != null) {
			Debug.Log ("Current Menu: " + currentMenu.name);
		} else {
			Debug.Log ("Current Menu: none");
		} */
	}

	void getScrollAccelerationTime() {
		// How long does it take to hit max scroll speed?
		//Debug.Log ("currentScrollSpeed: " + Mathf.Abs (currentScrollSpeed));
		if(Mathf.Abs(currentScrollSpeed) == maxScrollSpeed && !scrollAccelerationFound) {
			scrollAccelerationFound = true;
			scrollAccelerationTime = (Time.time - scrollStartTime);
			scrollStopTime += scrollAccelerationTime;
			//Debug.Log ("time for acceleration was: " + scrollAccelerationTime);
		}
	}

	void updateGUIObject(GUIObject guiObject) {
		if(guiObject.active) {
			if(scrollDirection) {
				guiObject.transform.position = new Vector3 (guiObject.transform.position.x - (currentScrollSpeed * Time.deltaTime) * GUIScrollSpeed, guiObject.transform.position.y, guiObject.transform.position.z);
			} else {
				guiObject.transform.position = new Vector3 (guiObject.transform.position.x + (currentScrollSpeed * Time.deltaTime) * GUIScrollSpeed, guiObject.transform.position.y, guiObject.transform.position.z);
			}
		}
	}

	void updateScroll() {
		if(scrollDirection) {
			background.material.mainTextureOffset = new Vector2(Mathf.Lerp(background.material.mainTextureOffset.x, background.material.mainTextureOffset.x + 1f, Time.deltaTime * currentScrollSpeed), 0f);
		} else {
			background.material.mainTextureOffset = new Vector2(Mathf.Lerp(background.material.mainTextureOffset.x, background.material.mainTextureOffset.x - 1f, Time.deltaTime * currentScrollSpeed), 0f);
		}
	}

	public void setGUIObjectActive(GUIObject guiObject, bool active) {
		float offsetAmount = 100f;
		if(active) {
			guiObject.transform.position = new Vector3 (guiObject.transform.position.x, guiObject.transform.position.y - offsetAmount, guiObject.transform.position.z);
		} else {
			guiObject.transform.position = new Vector3 (guiObject.transform.position.x, guiObject.transform.position.y + offsetAmount, guiObject.transform.position.z);
		}
		//guiObject.active = active;
	}

	// Update is called once per frame
	void Update () {
		deltaTime = Time.realtimeSinceStartup - previousStartUpTime;
		Debug.Log ("deltaTime_ALT: " + deltaTime);
		Debug.Log ("deltaTime_ACT: " + Time.deltaTime);
		previousStartUpTime = Time.realtimeSinceStartup;
		if(backgroundScroll) {
			if(Time.time < scrollStopTime) {
				getScrollAccelerationTime();
				if(Time.time > (scrollStopTime - scrollAccelerationTime)) {
					currentScrollSpeed = currentScrollSpeed - scrollAcceleration * Time.deltaTime;
					if(currentScrollSpeed < 0.1f) {
						//Debug.Log ("next menu arrived");
						currentScrollSpeed = 0f;
						if(deactiveLastMenu) {
							lastMenu.active = false;
							setGUIObjectActive(lastMenu, false); // Move menu up
							deactiveLastMenu = false;
						}
					}
				} else {
					if(currentScrollSpeed < maxScrollSpeed) {
						currentScrollSpeed = currentScrollSpeed + scrollAcceleration * Time.deltaTime;
					} else {
						currentScrollSpeed = maxScrollSpeed;
					}
				}
				updateGUIObject(MainMenu);
				updateGUIObject(Planet1Menu);
				updateGUIObject(OptionsMenu);
				updateGUIObject(LoadingMenu);
				updateScroll();
				/*
				if(scrollDirection) {
					MainMenu.transform.position = new Vector3(MainMenu.transform.position.x - (currentScrollSpeed * Time.deltaTime) * GUIScrollSpeed, MainMenu.transform.position.y, MainMenu.transform.position.z);
					Planet1Menu.transform.position = new Vector3(Planet1Menu.transform.position.x - (currentScrollSpeed * Time.deltaTime) * GUIScrollSpeed, Planet1Menu.transform.position.y, Planet1Menu.transform.position.z);
					background.material.mainTextureOffset = new Vector2(Mathf.Lerp(background.material.mainTextureOffset.x, background.material.mainTextureOffset.x + 1f, Time.deltaTime * currentScrollSpeed), 0f);
				} else {
					MainMenu.transform.position = new Vector3(MainMenu.transform.position.x + (currentScrollSpeed * Time.deltaTime) * GUIScrollSpeed, MainMenu.transform.position.y, MainMenu.transform.position.z);
					Planet1Menu.transform.position = new Vector3(Planet1Menu.transform.position.x + (currentScrollSpeed * Time.deltaTime) * GUIScrollSpeed, Planet1Menu.transform.position.y, Planet1Menu.transform.position.z);
					background.material.mainTextureOffset = new Vector2(Mathf.Lerp(background.material.mainTextureOffset.x, background.material.mainTextureOffset.x - 1f, Time.deltaTime * currentScrollSpeed), 0f);
				} */
			}
		}
	}
}
